有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在第三方应用程序中打开和关闭的Java程序监视器文件

我正在寻找一种方法,以不断监测当一个文件在我没有代码的特定第三方程序(例如Microsoft Word)中打开时,打开的特定文件的名称,以及该文件/程序何时关闭

了解这些信息的目的是,我可以跟踪打开某些“文档”或文件的时间,以便轻松记录各种项目的小时数

现在,我能想到的实现这一点的唯一方法是让我的Java程序发送命令来打开特定文件,然后记下这些文件何时关闭,但我更喜欢在后台运行,并且对用户来说尽可能简单透明

谢谢


共 (2) 个答案

  1. # 1 楼答案

    /  TO CHECK WHETHER A FILE IS OPENED 
        //  OR NOT (not for .txt files)
    
        //  the file we want to check
        String fileName = "C:\\Text.xlsx";
        File file = new File(fileName);
    
        // try to rename the file with the same name
        File sameFileName = new File(fileName);
    
        if(file.renameTo(sameFileName)){
            // if the file is renamed
            System.out.println("file is closed");    
        }else{
            // if the file didnt accept the renaming operation
            System.out.println("file is opened");
        }
    
  2. # 2 楼答案

    使用此选项检查文件是否在给定时间打开。定期检查此项,当状态更改时,文件已打开或关闭

    try {
        FileOutputStream stream = new FileOutputStream(file);
        // file is closed
    } catch(IOException e) {
        // file is open
    }